package top.ahaoao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import top.ahaoao.pojo.Account;
import top.ahaoao.service.AccountService;

import java.util.List;

/**
 * @author ：ahao
 * @version : 1.0
 * @date ：Created in 2020/11/12 16:48
 * @description ：账户表现层
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/saveAccount",method = {RequestMethod.POST})
    @ResponseBody
    public String saveAccount(Account account){
        System.out.println("表现层执行saveAccount方法");
        accountService.saveAccount(account);
        return "保存成功";
    }

    @RequestMapping("/findAll")
    public String findAll(Model model){
        //调用service的方法
        System.out.println("表现层：查询所有账户。。。");
        //调用service
        List<Account> list = accountService.findAll();
        for (Account account : list) {
            System.out.println(account);
        }
        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Account findById(int id){
        Account account = accountService.findById(id);
        return account;
    }

    @RequestMapping("/transfer")
    @ResponseBody
    public String transfer(String sourceName, String targetName, Float money){
        accountService.transfer(sourceName, targetName, money);
        return "转账成功";
    }
}

